package com.example.lin.day8_31;

import android.app.Application;
import android.app.Service;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Toast;

import com.example.lin.day8_31.db.DBManager;
import com.example.lin.day8_31.pojo.Student;
import com.example.lin.day8_31.utils.LogUtils;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ListView lv_items;
    private StudentAdapter studentAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv_items = findViewById(R.id.lv_items);
        registerForContextMenu(lv_items);
        studentAdapter = new StudentAdapter(this);
        lv_items.setAdapter(studentAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_add:
                showAddDialog();
                break;
            case R.id.menu_query:
                queryAllTest();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void queryAllTest() {
        List<Student> students = DBManager.getInstance().queryAll();
        studentAdapter.setList(students);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("操作");
        getMenuInflater().inflate(R.menu.menu_context_main, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        int position = menuInfo.position;//获得长按ListView 的Item的位置

        switch (item.getItemId()) {
            case R.id.menu_update:
                showUpdateDialog(position);
                break;
            case R.id.menu_delete:
                Student item1 = studentAdapter.getItem(position);
                long id = item1.getId();
                int delete = DBManager.getInstance().delete(id);
                Toast.makeText(this, delete > 0 ? "删除成功!" : "删除失败", Toast.LENGTH_SHORT).show();
                queryAllTest();
                break;
        }
        return super.onContextItemSelected(item);
    }

    private void showAddDialog() {
        View layout = getLayoutInflater().inflate(R.layout.view_dialog_update_1, null);
        final EditText etName = layout.findViewById(R.id.et_name);
        final EditText etAge = layout.findViewById(R.id.et_age);
        final RadioButton rbBoy = layout.findViewById(R.id.rb_boy);
        new AlertDialog.Builder(this).setTitle("添加学员")
                .setView(layout)
                .setPositiveButton("添加", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String name = etName.getText().toString();
                        String age = etAge.getText().toString();
                        boolean checked = rbBoy.isChecked();

                        Student student = new Student(name, checked ? "男" : "女", Integer.parseInt("".equals(age) ? "0" : age));
                        long insert = DBManager.getInstance().insert(student);
                        Toast.makeText(MainActivity.this, insert > 0 ? "添加成功!" : "添加失败!", Toast.LENGTH_SHORT).show();
                        queryAllTest();
                    }
                }).setNegativeButton("取消", null).show();
    }

    private void showUpdateDialog(int position) {

        final Student item = studentAdapter.getItem(position);

        View layout = getLayoutInflater().inflate(R.layout.view_dialog_update_1, null);
        final EditText etName = layout.findViewById(R.id.et_name);
        final EditText etAge = layout.findViewById(R.id.et_age);
        final RadioButton rb_boy = layout.findViewById(R.id.rb_boy);
        final RadioButton rb_girl = layout.findViewById(R.id.rb_girl);
        etName.setText(item.getName());
        etAge.setText(String.valueOf(item.getAge()));
        rb_girl.setChecked("女".equals(item.getSex()));
        new AlertDialog.Builder(this).setTitle("修改学员")
                .setView(layout)
                .setPositiveButton("确认修改", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String name = etName.getText().toString();
                        String age = etAge.getText().toString();
                        boolean checked = rb_boy.isChecked();
                        Student student = new Student(item.getId(),name, checked ? "男" : "女", Integer.parseInt("".equals(age) ? "0" : age));
                        long insert = DBManager.getInstance().update(student);
                        Toast.makeText(MainActivity.this, insert > 0 ? "修改成功!" : "修改失败!", Toast.LENGTH_SHORT).show();
                        queryAllTest();

                    }
                }).setNegativeButton("取消", null).show();

    }

}
